package com.mr.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.apache.commons.lang.StringUtils;

import com.mr.model.BaseObject;

public class ValidateUtil {
   private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
   private static Validator validator = factory.getValidator();
   public static <T extends BaseObject> List<String> validate(T bean){
   	Set<ConstraintViolation<T>> result = validator.validate(bean);   	
      List<String> msgList = new ArrayList();
      for(ConstraintViolation<T> iv: result){
         msgList.add(iv.getMessage());
      }
      return msgList;
   }
   public static <T extends BaseObject> String validate(T bean, String split){
      List<String> msgList = validate(bean);
      if(StringUtils.isBlank(split)) split="@@";
      return StringUtils.join(msgList.toArray(), split);
   }
   public static void main(String[] args) {
	String s = "    xx tt  ";
	System.out.println(s.trim());
}
}